package com.openroom.ui;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentTooltipListener;
import org.apache.pivot.wtk.LinkButton;

import com.openroom.db.DBHelper;
import com.openroom.event.ApplicationEvent;
import com.openroom.event.EventHandler;
import com.openroom.event.EventManager;
import com.openroom.event.EventType;
import com.openroom.model.ModelLocator;
import com.openroom.model.Photograph;

public class RatingComponent {
    protected LinkButton twoStarBtn;
    protected LinkButton oneStarBtn;
    protected LinkButton threeStarBtn;
    protected LinkButton fourStarBtn;
    protected LinkButton fiveStarBtn;

    public RatingComponent(LinkButton oneStarBtn, LinkButton twoStarBtn, LinkButton threeStarBtn,
            LinkButton fourStarBtn, LinkButton fiveStarBtn) {
        super();
        this.twoStarBtn = twoStarBtn;
        this.oneStarBtn = oneStarBtn;
        this.threeStarBtn = threeStarBtn;
        this.fourStarBtn = fourStarBtn;
        this.fiveStarBtn = fiveStarBtn;
    }

    public void initialize() {
        setUserData();

        addListener();

        addEventHandlers();

        addHandlersForRetrievedImage();
        fiveStarBtn.getComponentTooltipListeners().add(new ComponentTooltipListener() {

            @Override
            public void tooltipTriggered(Component component, int x, int y) {
            }
        });
    }

    protected void addHandlersForRetrievedImage() {
        EventManager.getInstance().listenToEventType(new EventHandler() {
            @Override
            public void handle(ApplicationEvent event) {
                Photograph p = ModelLocator.getInstance().getCurrentlySelectedImage();
                if (p == null)
                    return;
                Photograph selectImage = DBHelper.getInstance().selectImage(p.getPhotoId());
                int ratingValue = selectImage.getRatingValue();

                switch (ratingValue) {
                case 0:
                case -1:
                    RatingEventHandler.setColor(oneStarBtn, false);
                    RatingEventHandler.setColor(twoStarBtn, false);
                    RatingEventHandler.setColor(threeStarBtn, false);
                    RatingEventHandler.setColor(fourStarBtn, false);
                    RatingEventHandler.setColor(fiveStarBtn, false);
                    break;
                case 1:
                    RatingEventHandler.setColor(oneStarBtn, true);
                    RatingEventHandler.setColor(twoStarBtn, false);
                    RatingEventHandler.setColor(threeStarBtn, false);
                    RatingEventHandler.setColor(fourStarBtn, false);
                    RatingEventHandler.setColor(fiveStarBtn, false);
                    break;
                case 2:
                    RatingEventHandler.setColor(oneStarBtn, true);
                    RatingEventHandler.setColor(twoStarBtn, true);
                    RatingEventHandler.setColor(threeStarBtn, false);
                    RatingEventHandler.setColor(fourStarBtn, false);
                    RatingEventHandler.setColor(fiveStarBtn, false);
                    break;
                case 3:
                    RatingEventHandler.setColor(oneStarBtn, true);
                    RatingEventHandler.setColor(twoStarBtn, true);
                    RatingEventHandler.setColor(threeStarBtn, true);
                    RatingEventHandler.setColor(fourStarBtn, false);
                    RatingEventHandler.setColor(fiveStarBtn, false);
                    break;
                case 4:
                    RatingEventHandler.setColor(oneStarBtn, true);
                    RatingEventHandler.setColor(twoStarBtn, true);
                    RatingEventHandler.setColor(threeStarBtn, true);
                    RatingEventHandler.setColor(fourStarBtn, true);
                    RatingEventHandler.setColor(fiveStarBtn, false);
                    break;
                case 5:
                    RatingEventHandler.setColor(oneStarBtn, true);
                    RatingEventHandler.setColor(twoStarBtn, true);
                    RatingEventHandler.setColor(threeStarBtn, true);
                    RatingEventHandler.setColor(fourStarBtn, true);
                    RatingEventHandler.setColor(fiveStarBtn, true);
                    break;
                default:
                    break;
                }
            }
        }, EventType.CURRENT_IMAGE_CHANGED, EventType.CURRENT_RATING_CHANGE);
    }

    protected void addEventHandlers() {
        EventManager.getInstance().listenToEventType(EventType.CURRENT_RATING_CHANGE,
                new RatingEventHandler(oneStarBtn, twoStarBtn, threeStarBtn, fourStarBtn, fiveStarBtn));
    }

    protected void addListener() {
        oneStarBtn.getButtonPressListeners().add(new RatingButtonListener());
        twoStarBtn.getButtonPressListeners().add(new RatingButtonListener());
        threeStarBtn.getButtonPressListeners().add(new RatingButtonListener());
        fourStarBtn.getButtonPressListeners().add(new RatingButtonListener());
        fiveStarBtn.getButtonPressListeners().add(new RatingButtonListener());
    }

    protected void setUserData() {
        oneStarBtn.getUserData().put("RATING_VALUE", 1);
        twoStarBtn.getUserData().put("RATING_VALUE", 2);
        threeStarBtn.getUserData().put("RATING_VALUE", 3);
        fourStarBtn.getUserData().put("RATING_VALUE", 4);
        fiveStarBtn.getUserData().put("RATING_VALUE", 5);
    }
}
